<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>碳排放计算器</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
    <div class="container">
        <h1>碳排放计算器</h1>
        <form method="POST">
            <input type="hidden" name="timestamp" value="{{ now }}">
            <div class="form-group">
                <label for="processEmission">工艺排放 (吨):</label>
                <input type="number" id="processEmission" name="processEmission" required>
            </div>
            <div class="form-group">
                <label for="energyConsumption">能源消耗 (吨):</label>
                <input type="number" id="energyConsumption" name="energyConsumption" required>
            </div>
            <div class="form-group">
                <label for="resourceConsumption">资源消耗 (吨):</label>
                <input type="number" id="resourceConsumption" name="resourceConsumption" required>
            </div>
            <div class="form-group">
                <label for="processFactor">工艺排放因子:</label>
                <input type="number" id="processFactor" name="processFactor" value="0.22" required>
            </div>
            <div class="form-group">
                <label for="energyFactor">能源消耗因子:</label>
                <input type="number" id="energyFactor" name="energyFactor" value="0.56" required>
            </div>
            <div class="form-group">
                <label for="resourceFactor">资源消耗因子:</label>
                <input type="number" id="resourceFactor" name="resourceFactor" value="0.33" required>
            </div>
            <button type="submit" class="btn">计算碳排放</button>
        </form>

        <div id="result">
            {% if history_records %}
                <h2>历史记录</h2>
                <ul>
                    {% for record in history_records %}
                        <li>
                            <a href="#" onclick="showDetails({{ record[0] }})">{{ record[1] }}</a>
                            <div id="details-{{ record[0] }}" class="details" style="display: none;">
                                <p>工艺排放: {{ record[2] }} 吨</p>
                                <p>能源消耗: {{ record[3] }} 吨</p>
                                <p>资源消耗: {{ record[4] }} 吨</p>
                                <p>工艺排放因子: {{ record[5] }}</p>
                                <p>能源消耗因子: {{ record[6] }}</p>
                                <p>资源消耗因子: {{ record[7] }}</p>
                                <p>总碳排放量: {{ record[8] }} 吨</p>
                            </div>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
        </div>

        <div class="buttons">
            <form method="POST" action="{{ url_for('clear_history') }}">
                <button type="submit" class="btn btn-clear">清除历史</button>
            </form>
            <form method="POST" action="{{ url_for('save_history') }}">
                <button type="submit" class="btn btn-save">保存历史</button>
            </form>
        </div>
    </div>

    <script>
        function showDetails(id) {
            const details = document.getElementById(`details-${id}`);
            details.style.display = details.style.display === 'none' ? 'block' : 'none';
        }
    </script>
</body>
</html>